
<%@ include file="/common/taglibs.jsp"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>

<!--引用的标签页js库-->
<script type="text/javascript" src="<%=jsPath%>/jquery/jquery-asTabs.min.js"></script>
<link rel="stylesheet" type="text/css" href="<%=jsPath%>/jquery/css/jquery-asTabs.css" />

<script type="text/javascript" src="<%=jsPath%>/jquery/jquery.timers.js"></script><!--定时器-->
<script type="text/javascript" src="<%=jsPath%>/terminalCommand.js?v=34"></script><!--终端命令结果查询-->
<script>

    /*
    页面功能：设置通道列表参数，查询参数
	修改代码：
	1.新定义变量TerminalCommmand，为命令支持变量
	2.重定义id：
	原：null		现：capCommandMsg
	原：null		现：capBtnQuery
	原：		现：
	*/

	var tc2ChannelAvparam = new TerminalCommand2();
    var timerName = "channelAvParamTimer";
    ///////////////////////////////////////////////////////////////
    tc2ChannelAvparam.messageDiv = $("#capCommandMsg");
	tc2ChannelAvparam.timerName = timerName;
	//tc2ChannelAvparam.commandButton = $("#capBtnQuery");

    //代码重定义
    //var timerName = "timerForQuery";
    var operation = "";//操作类型是查询query还是修改modify
    var times=30;
    var currentChannel = 0;//当前的通道

    //存放每个通道的音视频参数，键值是通道号
    var avParamMap = {};
    //当查询终端参数命令执行成功时
    function onQueryCommandSuccess(result){
        if (result.success) {
            var p = result.data[0];
            var strArray = p.value.split("|");
            $("#channel").empty(); //清空通道下拉框
            for(var m = 0; m < strArray.length; m++)
			{
			    var strParams = strArray[m];
			    var strParamArray = strParams.split(",");
			    var channelId = strParamArray[0];
			    avParamMap[channelId] = strParamArray;
                $("#channel").append('<option value="'+channelId+'">通道'+channelId+'</option>');
                if(m == 0)
				{
                    currentChannel = channelId;
				    showAvParamByChannel(strParamArray);
				}
            }
        }

    }

    function showAvParamByChannel(strArray)
	{
        var i = 1;
        Utility.setComboboxDefaultValue("realTimeCodecMode", strArray[i++]);
        Utility.setComboboxDefaultValue("resolution", strArray[i++]);
        $("#keyFrameInterval").val(strArray[i++]);
        $("#frameRate").val(strArray[i++]);
        $("#bitRate").val(strArray[i++]);
        Utility.setComboboxDefaultValue("storageCodecMode", strArray[i++]);
        Utility.setComboboxDefaultValue("storageResolution", strArray[i++]);
        $("#storageKeyFrameInterval").val(strArray[i++]);
        $("#storageFrameRate").val(strArray[i++]);
        $("#storageBitRate").val(strArray[i++]);
        var osdValue = parseInt(strArray[i++]);
        //将osd值由10进制转二进制
        var str = osdValue.toString(2);
        var bitArray = str.split("").reverse();
        var osdArray = [];

        $.each(bitArray, function(i,v)
        {
            if(v == "1")
                osdArray.push(i);
        })
        $("#osdCombo").combotree('setValues',osdArray);
        var selectedValues = osdArray.join(",");
        $("#osdText").val(selectedValues);
	}


    function fillAvParam(channel)
    {
        if(channel == 0)
            return;
        //var channel = $("#channel").val();
        var strArray = [];
        var i = 0;
        strArray[i++] = channel;
        strArray[i++] = $("#realTimeCodecMode").val();
        strArray[i++] = $("#resolution").val();
        strArray[i++] = $("#keyFrameInterval").val();
        strArray[i++] = $("#frameRate").val();
        strArray[i++] = $("#bitRate").val();
        strArray[i++] = $("#storageCodecMode").val();
        strArray[i++] = $("#storageResolution").val();
        strArray[i++] = $("#storageKeyFrameInterval").val();
        strArray[i++] = $("#storageFrameRate").val();
        strArray[i++] = $("#storageBitRate").val();
        var strOsd = $("#osdText").val();
        var osdArray = strOsd.split(",");
        var bitArray = [];
        for(var m = 0; m < 16; m++)
		{
		    bitArray.push(0);
		}

		for(var m = 0; m < osdArray.length; m++)
		{
		    var index = parseInt(osdArray[m]);
		    bitArray[16 - 1 - index] = 1;
		}

		var strBit = bitArray.join("");
        var osdValue = parseInt(strBit, 2);
        strArray[i++] = osdValue;

        avParamMap[channel] = strArray;
    }

    /**
     * 下发参数查询指令
     */
    function sendParamQueryCommand()
    {
        operation = "query";
        var url = "<%=ApplicationPath%>/videoTerminalParam/sendParamQueryCommand.action";
        var param = {paramId : 0x0077,vehicleId:${vehicleId}};
        $.getJSON(url, param, function(result)
        {
            if (result.success) {
                var commandId = result.data; //下发成功后，获取到命令Id
                tc2ChannelAvparam.commandButton = $("#capBtnQuery");
                var url = "<%=ApplicationPath%>/terminalParam/queryTerminalParam.action";
                tc2ChannelAvparam.times=190;

                tc2ChannelAvparam.startQueryResult(commandId,url,onQueryCommandSuccess);//命令下发成功,根据命令id,开始尝试获取检索结果

            }else {
                $("#capCommandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
                //停止所有的在$('body')上定时器
                $('#capBody').stopTime (timerName);
            }
        });
    }


    function startQueryResult(commandId)
    {
        times=60;
        $('#capBtnQuery').attr("disabled","disabled");
        $("#capBtnQuery").val("等待终端返回查询结果("+times+" )");
        $('#capBody').oneTime('500ms', timerName,function(){
            queryResult(commandId); //查询命令的返回结果
        });
    }
    //根据下发参数查询命令ID，来查询命令返回的参数结果
    function queryResult(commandId)
    {
        times--;
        $('#capBtnQuery').attr("disabled","disabled");
        $("#capBtnQuery").val("等待终端返回查询结果("+times+" )");


        //alert(times);
        var url = "<%=ApplicationPath%>/takePicture/getPictureConfig.action";
        //var commandId = 0;
        var params = {commandId:commandId,vehicleId:${vehicleId}};
        $.getJSON(url, params,
            function(result){
                if(result.success == true)
                {
                    $("#capCommandMsg").html('查询成功');
                    //返回的查询结果，填充到input输入框中
                    if(console.log)
                        console.log(result.message);
                    setPhotoConfig(result.message);//获取参数后，填充到表单中
                    $("#capBtnQuery").val("参数查询");
                    $('#capBtnQuery').attr("disabled",false);


                    //停止所有的在$('body')上定时器
                    $("#capBody").stopTime (timerName);
                    //$('#btnQuery').removeAttr("disabled");
                }else {
                    if(times == 1)
                    {
                        $("#capCommandMsg").html("命令执行超时，没有返回结果,请重新查询");
                        $("#capBtnQuery").val("参数查询");
                        $('#capBtnQuery').attr("disabled",false);
                        $('#capBody').stopTime (timerName);
                    }else
                    {
                        $("#capCommandMsg").html(result.message);
                        //最多执行10次
                        $('#capBody').oneTime('900ms', timerName,function(){
                            queryResult(commandId); //查询命令的返回结果
                        });
                    }

                }
            }
        );
    }

    function configAvParam()
	{
	    if(currentChannel == 0)
		{
            currentChannel = $("#channel").val();
            if(currentChannel == 0)
			{
                $.messager.alert('提示','请选择通道!');
			    return;
			}
		}
	    fillAvParam(currentChannel);
	    var allChannelParamArray = [];
	    for(var ch in avParamMap)
		{
		    var chParamArray = avParamMap[ch];
            allChannelParamArray.push(chParamArray.join("&"));
		}
		var strParam = allChannelParamArray.join("|");
	    var url = "<%=ApplicationPath%>/videoTerminalParam/configChannelAvParam.action";
	    var param = {vehicleId : ${vehicleId}, avParam:strParam};
	    $.getJSON(url, param, function(result)
		{
            tc2ChannelAvparam.messageDiv = $("#capCommandMsg");
            if (result.success) {
                var commandId = result.data; //下发成功后，获取到命令Id
                if(commandId != null)
                {
                    //var url = "<%=ApplicationPath%>/takePicture/getPictureResult.action";
                    tc2ChannelAvparam.times=100;
                    tc2ChannelAvparam.commandButton = $("#capBtnConfig");
                    tc2ChannelAvparam.startQueryResult(commandId);//命令下发成功,根据命令id,开始尝试获取检索结果
                }else
                {
                    //如果类型是保存到终端，则不需要检测上传
                    tc2ChannelAvparam.startQueryResult(commandId);
                }
            }
            else {
                $("#capCommandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
                //$('body').stopTime ();
            }
		})
	}

    function onOsdComboTreeChecked(selectedValues)
    {
        $("#osdText").val(selectedValues);
    }

    $().ready(function() {
        $("#capEntityForm").validate(); //初始化验证信息
        $("#channel").fillVehicleChannels({vehicleId:"${vehicleId}"},"0");
        $('#channel').change(function(){
            
            //alert($(this).children('option:selected').val());
            fillAvParam(currentChannel);//保存之前的通道
            var ch=$(this).children('option:selected').val();//这就是selected的值
            var avParam = avParamMap[ch];
            showAvParamByChannel(avParam);
            currentChannel = ch;
        })
        //ajax填充下拉框数据 填充监听类型 选项
        $("#osdCombo").initComboTree({params:{category:"OsdText"},onCheck:onOsdComboTreeChecked,selectedValues:[0,1,2]});

        $("#capBtnQuery").click(function()
        {
            sendParamQueryCommand();//下发参数查询命令
        });
        $("#capBtnConfig").click(function()
        {
            configAvParam();//下发参数查询命令
        });

    });
    var hide = true;
    function showAdvanced()
    {
        if(hide)
        {
            $(".advanced").show();
        }else
            $(".advanced").hide();
        hide = hide != true;
    }


</script>
<BODY id="capBody">
<form id="capEntityForm" name="capEntityForm"
	  action='<%=ApplicationPath%>/videoTerminalParam/configChannelAvParam.action' method="POST">

	<input type="hidden"  name="vehicleId"  id="vehicleId" value="${vehicleId}"/>
	<input type="hidden"  name="osdText"  id="osdText" value="0,1,2"/>
	<input type="hidden"  name="sendCommand"  id="sendCommand" value="false"/>
	<table width="100%"  class="TableBlock">
		<tbody><tr>
			<td colspan="4" style="font-weight: bold; background: #EFEFEF;" height="25">通道音视频参数设置
				选择通道:<select id="channel" name="channel">
				</select>

				<button id="capBtnConfig" type="button" class="button green medium " ><i class="fa fa-cog" style="margin-right:3px;"></i> 设置参数</button>

				<button id="capBtnQuery" type="button" class="button blue medium " ><i class="fa fa-reply-all" style="margin-right:3px;"></i> 查询参数</button>

				<span id="capCommandMsg" class="commandMsg"></span>
			</td>
		</tr>
		<tr>
			<td align="right">实时流编码模式:</td>
			<td >
				<select id="realTimeCodecMode" name="realTimeCodecMode">
					<option value="0">CBR(固定码率)</option>
					<option value="1">VBR(可变码率)</option>
					<option value="2">ABR(平均码率)</option>
				</select>
			</td>
			<td align="right">实时流分辨率:</td>
			<td >
				<select id="resolution" name="resolution">
					<option value="0">QCIF</option>
					<option value="1">CIF</option>
					<option value="2">WCIF</option>
					<option value="3">DI</option>
					<option value="4">WDI</option>
					<option value="5">720P</option>
					<option value="6">1080P</option>
				</select>
			</td>
		</tr>

		<tr>

			<td align="right">实时流关键帧间隔(帧)
				:</td>
			<td>
				<input type="text" id="keyFrameInterval"   name="keyFrameInterval" maxlength="16" size="20"  value="10"
					   class="required digits min:1" style="width:80px">
			</td>
			<td align="right">实时流目标帧率(帧/s):</td>
			<td ><input id="frameRate" name="frameRate" class="required digits"  value="10" maxlength="16" size="20" style="width:100px"  class="required digits">
			</td>
		</tr>

		<tr>
			<td align="right">实时流目标码率(kbps):</td>
			<td >
				<input type="text" id="bitRate"   name="bitRate" maxlength="16" size="20"  value="10"
					   class="required digits min:1" style="width:80px">

			</td>
			<td align="right">存储流编码模式:</td>
			<td >
				<select id="storageCodecMode" name="storageCodecMode">
					<option value="0">CBR(固定码率)</option>
					<option value="1">VBR(可变码率)</option>
					<option value="2">ABR(平均码率)</option>
				</select>
			</td>

		</tr>

		<tr class="advanced">

			<td align="right">存储流分辨率:</td>
			<td >
				<select id="storageResolution" name="storageResolution">
					<option value="0">QCIF</option>
					<option value="1">CIF</option>
					<option value="2">WCIF</option>
					<option value="3">DI</option>
					<option value="4">WDI</option>
					<option value="5">720P</option>
					<option value="6">1080P</option>
				</select>
			</td>
			<td align="right">存储流关键帧间隔(帧)
				:</td>
			<td>
				<input type="text" id="storageKeyFrameInterval"   name="storageKeyFrameInterval" maxlength="16" size="20"  value="10"
					   class="required digits min:1" style="width:80px">
			</td>
		</tr>

		<tr  class="advanced">
			<td align="right">存储流目标帧率(帧/s):</td>
			<td ><input id="storageFrameRate" name="storageFrameRate" class="required digits"  value="10" maxlength="16" size="20" style="width:100px"  class="required digits">
			</td>
			<td align="right">存储流目标码率(kbps):</td>
			<td >
				<input type="text" id="storageBitRate"   name="storageBitRate" maxlength="16" size="20"  value="10"
					   class="required digits min:1" style="width:80px">

			</td>
		</tr>

		<tr  class="advanced">
			<td align="right">OSD字幕叠加设置:</td>
			<td colspan="3">
				<select id="osdCombo" class="easyui-combotree" style="width: 280px; height: 24px;">
				</select>
			</td>

		</tr>


		</tbody></table>
</form>

</BODY>
</HTML>
